#include "node_item_base.h"

NodeItemBase::NodeItemBase()
{

}

NodeItemBase::NodeItemBase(QString title):QGraphicsItem()
{
    this->title = title;
    title_color= new QColor("#aaaaaa");
    text = new QGraphicsTextItem(this->title,this);  // 注意设置父级为自身
    text->setDefaultTextColor(*this->title_color);  //文本颜色
    text->setPos(4,2);
    //text->setTextWidth(-1);

    // 嵌入的Content
    this->proxyContent = new QGraphicsProxyWidget(this);
    //this->content = new NodeContentNumber();
    //this->content->setGeometry(edge_size, title_height+0.5*edge_size,
    //                           width - 2*edge_size,height - 2*edge_size - title_height);
    //将nodeContent嵌入proxyContent
    //this->proxyContent->setWidget(this->content);

    // opengl 嵌入 proxyContent
//    CoreFunctionWidget *gl = new CoreFunctionWidget();
//    this->proxyContent->setWidget(gl);



    this->setFlag(QGraphicsItem::ItemIsSelectable);
    this->setFlag(QGraphicsItem::ItemIsMovable);
}

NodeItemBase::NodeItemBase(const NodeItemBase &nodeItem)
{
    Q_UNUSED(nodeItem);
}

NodeItemBase::~NodeItemBase()
{
    delete title_color;
    delete text;
    delete proxyContent;
    //delete content;
    title_color = NULL;
    text = NULL;
    proxyContent = NULL;
    //content = NULL;
}

void NodeItemBase::operator=(const NodeItemBase &nodeItem)
{
    Q_UNUSED(nodeItem);
}

void NodeItemBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);
    QPainterPath *path_title = new QPainterPath();
    path_title->setFillRule(Qt::WindingFill);
    path_title->addRoundedRect(0,0,width,title_height,edge_size,edge_size);//创建椭圆
    path_title->addRect(0,title_height-edge_size,edge_size,edge_size);//给椭圆左侧增加Rect遮住倒角
    path_title->addRect(width-edge_size,title_height-edge_size,edge_size,edge_size);//给椭圆右侧增加Rect遮住倒角
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor("#FF313131")); //E3212121
    painter->drawPath(path_title->simplified());

    QPainterPath *path_data = new QPainterPath();
    path_data->setFillRule(Qt::WindingFill);
    path_data->addRoundedRect(0,title_height,width,height-title_height,edge_size,edge_size);//创建椭圆
    path_data->addRect(0,title_height,edge_size,edge_size);//给椭圆左侧增加Rect遮住倒角
    path_data->addRect(width-edge_size,title_height,edge_size,edge_size);//给椭圆右侧增加Rect遮住倒角
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor("#E3212121"));
    painter->drawPath(path_data->simplified());



    QPainterPath *path_outline =new QPainterPath();
    path_outline->addRoundedRect(0,0,width,height,edge_size,edge_size);
    if(!this->isSelected()){
        painter->setPen(QPen(QColor("#aaaaaa")));
    }
    else{
        painter->setPen(QPen(QColor("#FFFFA637")));
    }
    painter->setBrush(Qt::NoBrush);
    painter->drawPath(path_outline->simplified());
}

QRectF NodeItemBase::boundingRect() const
{
    return QRectF(
        0,
        0,
        width,height
//        2 * edge_size * width,
//        2 * edge_size * height
                );
}

//bool NodeItemBase::contains(const QPointF &point) const
//{
//    int x = this->pos().x();
//    int y = this->pos().y();
////    int x = this->parentItem()->x()+this->pos().x();
////    int y = this->parentItem()->y()+this->pos().y();
//    qDebug()<<"collide"<<x<<y;
////    if(point.x()>x-radius && point.x()<x+radius && point.y()>y-radius && point.y()<y+radius){
////        return true;
////    }
//    if(point.x()>x && point.x()<x + width &&
//            point.y()>y && point.y()<y+height){
//        return true;
//    }
//    return false;
//}

void NodeItemBase::SetTitle(QString title)
{
    this->title = title;
    this->text->setPlainText(title);
}

double NodeItemBase::GetData()
{
    qDebug()<<"GetNodeItemBase";
    return 0;
}

void NodeItemBase::SetData(QString str)
{
    Q_UNUSED(str);
    qDebug()<<"SetNodeItemBase";
}

NumberData NodeItemBase::GetNumberData()
{
    qDebug()<<"GetNodeItemBase";
    NumberData data;
    return data;
}

void NodeItemBase::SetNumberData(NumberData data)
{
    Q_UNUSED(data);
    qDebug()<<"SetNodeItemBase";
}
///////////////////////



NodeItemNumber::NodeItemNumber():NodeItemBase("Number")
{
    NodeItemBase::height = 80;
    this->content = new NodeContentNumber();
    this->content->setGeometry(edge_size, title_height+0.5*edge_size,
                               width - 2*edge_size,height - 2*edge_size - title_height);
    NodeItemBase::proxyContent->setWidget(this->content);


}

double NodeItemNumber::GetData()
{
    return this->content->GetData();

}

void NodeItemNumber::SetData(QString str)
{
    qDebug()<<"Set NodeItemTest";
    this->content->SetData(str);
}

NumberData NodeItemNumber::GetNumberData()
{
    return this->content->GetNumberData();
}

void NodeItemNumber::SetNumberData(NumberData data)
{
    QString text = QString::number(data.data[0],10,4);
    this->content->SetData(text);
}

NodeItemOutput::NodeItemOutput():NodeItemBase("Output")
{
    NodeItemBase::height = 80;
    this->content = new NodeContentOutput();
    this->content->setGeometry(edge_size, title_height+0.5*edge_size,
                               width - 2*edge_size,height - 2*edge_size - title_height);
    NodeItemBase::proxyContent->setWidget(this->content);

}

double NodeItemOutput::GetData()
{
    return this->content->GetData();

}

void NodeItemOutput::SetData(QString str)
{
    qDebug()<<"Set NodeItemOutput";
    this->content->SetData(str);
}

NumberData NodeItemOutput::GetNumberData()
{
    return this->content->GetNumberData();
}

void NodeItemOutput::SetNumberData(NumberData data)
{
    QString text = QString::number(data.data[0],10,4);
    this->content->SetData(text);
}



NodeItemAdd::NodeItemAdd():NodeItemBase("Add")
{
    NodeItemBase::height = 80;
    this->content = new NodeContentAdd();
    this->content->setGeometry(edge_size, title_height+0.5*edge_size,
                               width - 2*edge_size,height - 2*edge_size - title_height);
    NodeItemBase::proxyContent->setWidget(this->content);

}
double NodeItemAdd::GetData()
{
    return this->content->GetData();

}

void NodeItemAdd::SetData(QString str)
{
    qDebug()<<"Set NodeItemOutput";
    this->content->SetData(str);
}

NumberData NodeItemAdd::GetNumberData()
{
    return this->content->GetNumberData();
}

void NodeItemAdd::SetNumberData(NumberData data)
{
    this->content->SetNumberData(data);
}




NodeItemSub::NodeItemSub():NodeItemBase("Sub")
{
    NodeItemBase::height = 80;
    this->content = new NodeContentSub();
    this->content->setGeometry(edge_size, title_height+0.5*edge_size,
                               width - 2*edge_size,height - 2*edge_size - title_height);
    NodeItemBase::proxyContent->setWidget(this->content);

}
double NodeItemSub::GetData()
{
    return this->content->GetData();

}

void NodeItemSub::SetData(QString str)
{
    qDebug()<<"Set NodeItemOutput";
    this->content->SetData(str);
}

NumberData NodeItemSub::GetNumberData()
{
    return this->content->GetNumberData();
}

void NodeItemSub::SetNumberData(NumberData data)
{
    this->content->SetNumberData(data);
}




NodeItemMul::NodeItemMul():NodeItemBase("Mul")
{
    NodeItemBase::height = 80;
    this->content = new NodeContentMul();
    this->content->setGeometry(edge_size, title_height+0.5*edge_size,
                               width - 2*edge_size,height - 2*edge_size - title_height);
    NodeItemBase::proxyContent->setWidget(this->content);

}
double NodeItemMul::GetData()
{
    return this->content->GetData();

}

void NodeItemMul::SetData(QString str)
{
    qDebug()<<"Set NodeItemOutput";
    this->content->SetData(str);
}

NumberData NodeItemMul::GetNumberData()
{
    return this->content->GetNumberData();
}

void NodeItemMul::SetNumberData(NumberData data)
{
    this->content->SetNumberData(data);
}



NodeItemDiv::NodeItemDiv():NodeItemBase("Div")
{
    NodeItemBase::height = 80;
    this->content = new NodeContentDiv();
    this->content->setGeometry(edge_size, title_height+0.5*edge_size,
                               width - 2*edge_size,height - 2*edge_size - title_height);
    NodeItemBase::proxyContent->setWidget(this->content);

}
double NodeItemDiv::GetData()
{
    return this->content->GetData();

}

void NodeItemDiv::SetData(QString str)
{
    qDebug()<<"Set NodeItemOutput";
    this->content->SetData(str);
}

NumberData NodeItemDiv::GetNumberData()
{
    return this->content->GetNumberData();
}

void NodeItemDiv::SetNumberData(NumberData data)
{
    this->content->SetNumberData(data);
}


NodeGraphicsShow::NodeGraphicsShow():NodeItemBase("Show")
{
// 目前并不需要Content，因为其他节点的数据在Content里，但是该节点目前没有数据
    NodeItemBase::height = 100;
    NodeItemBase::width = 100;
    this->content = new NodeContentNumber();
    this->content->setGeometry(edge_size, title_height+0.5*edge_size,
                               width - 2*edge_size,height - 2*edge_size - title_height);
    NodeItemBase::proxyContent->setWidget(this->content);
    NodeOpenGLTest *gl = new NodeOpenGLTest();
    gl->resize(height,width);
    this->proxyContent->setWidget(gl);
}

double NodeGraphicsShow::GetData()
{

}

void NodeGraphicsShow::SetData(QString str)
{

}

NumberData NodeGraphicsShow::GetNumberData()
{

}

void NodeGraphicsShow::SetNumberData(NumberData data)
{

}
